
package doc2db.interfaces.action.createTable;

import doc2db.UserProp;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author yura
 */
public class TableColumn extends JPanel{
    private static int num = 1;
    private JLabel column_name = new JLabel("имя данной колонки:   ");
    private JLabel data_type = new JLabel  ("тип данных колонки:   ");
    private JLabel  isDefaultD = new JLabel("значение по умолчянию:");
    private JTextField name = new JTextField("new Head " + num, 32);
    private JTextField type = new JTextField("text", 32);
    private JTextField defaultD = new JTextField("-", 32);
    private JCheckBox isNotNull = new JCheckBox("not null", false);
    private JCheckBox isUnique  = new JCheckBox("unique", false);
    private JCheckBox isAutoIncrement  = new JCheckBox("auto increment", false);

    public TableColumn () {
        super(new BorderLayout());
        this.setNewFont(UserProp.textFont, UserProp.paneFont);
        this.initComponent();
        setVisible(true);
        setBorder(BorderFactory.createEtchedBorder());
        num ++;
    }

    public void setNewFont(Font textf, Font paneF){
        name.setFont(textf);
        type.setFont(textf);
        defaultD.setFont(textf);
        column_name.setFont(paneF);
        data_type.setFont(paneF);
        isDefaultD.setFont(paneF);
        isNotNull.setFont(paneF);
        isUnique.setFont(paneF);
        isAutoIncrement.setFont(paneF);
    }

    private boolean isD(){
        if(isUnique.isSelected())
            return true;
        else if(isAutoIncrement.isSelected())
            return true;
        else return isNotNull.isSelected();
    }

    private void initComponent(){
        isNotNull.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                defaultD.setEnabled(!isD());
            }
        });
        isUnique.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                defaultD.setEnabled(!isD());
            }
        });
        isAutoIncrement.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                defaultD.setEnabled(!isD());
                type.setText("int");
                isUnique.setSelected(true);
            }
        });
        
        name.setMaximumSize(name.getPreferredSize());
        type.setMaximumSize(type.getPreferredSize());
        defaultD.setMaximumSize(defaultD.getPreferredSize());
        isDefaultD.setToolTipText("<html><table bgcolor=yellow border=1><tr><th>" +
                                  "<font size=\"4\" face=\"Verdana\">" +
                                  "Если оставить это поле пустым, <br>" +
                                  "или вставить прочерк \'-\' -<br>" +
                                  " оно не будит учтино");

        int h = 4;
        Box b = Box.createVerticalBox();
        b.add(column_name);
        b.add(name);
        b.add(Box.createVerticalStrut(h));
        b.add(data_type);
        b.add(type);
        b.add(Box.createVerticalStrut(h));
        b.add(isDefaultD);
        b.add(defaultD);
        b.add(Box.createVerticalStrut(h));
        b.add(isNotNull);
        b.add(Box.createVerticalStrut(h));
        b.add(isUnique);
        b.add(Box.createVerticalStrut(h));
        b.add(isAutoIncrement);
        b.add(Box.createVerticalStrut(h));

        add(b);
    }

    public String getText(){
        StringBuffer ret = new StringBuffer();

        ret.append(" \"");
        ret.append(name.getText().trim().replaceAll("\"", "\\\""));
        ret.append("\" ");
        ret.append(type.getText().trim().replaceAll("\"", "\\\""));

        if (!isNotNull.isSelected()) {
            if (! isD()) {
                if (!(defaultD.getText().equals("") || defaultD.getText().equals("-"))) {
                    ret.append(" DEFAULT \'");
                    ret.append(defaultD.getText().trim().replaceAll("\"", "\\\""));
                    ret.append("\' ");
                }
            }
        } else {
            ret.append(" NOT NULL ");
        }
        
        if(isUnique.isSelected()){
            ret.append(" UNIQUE ");
        }

        return ret.toString();
    }

    public String getColName(){
        return name.getText();
    }

    public boolean isAutoIncrement(){
        return isAutoIncrement.isSelected();
    }

}
